package com.cristina.attention.handler.chat.group;

import com.cristina.attention.annotation.SyncChat;
import com.cristina.attention.codec.Invocation;
import com.cristina.attention.dispatcher.MessageHandler;
import com.cristina.attention.message.chat.ChatSendResponse;
import com.cristina.attention.manager.NettyChannelManager;
import com.cristina.attention.message.chat.ChatRedirect;
import com.cristina.attention.message.chat.group.SendGroupChat;
import com.cristina.attention.utils.AttentionCommon;
import io.netty.channel.Channel;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 群聊
 * @Author superman
 * @Date 2021/9/29 13:38
 **/
@Component
public class SendGroupChatHandler implements MessageHandler<SendGroupChat> {

    @Resource
    private NettyChannelManager nettyChannelManager;

    @Override
    @SyncChat(AttentionCommon.TYPE_GROUP)
    public void execute(Channel channel, SendGroupChat message) {
        ChatSendResponse response = new ChatSendResponse()
                .setMessageId(message.getMessageId())
                .setCode(200);
        channel.writeAndFlush(new Invocation(ChatSendResponse.TYPE, response));

        ChatRedirect chatRedirect = ChatRedirect.builder()
                .messageId(message.getMessageId())
                .content(message.getContent())
                .sendId(String.valueOf(message.getSendId())).build();

        nettyChannelManager.sendGroup(message.getGroupId(), chatRedirect);
    }

    @Override
    public String getType() {
        return ChatSendResponse.TYPE;
    }
}
